package com.cmrx.model.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cmrx.model.entity.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {

    /** token秘钥，请勿泄露，请勿随便修改 backups:CMrx4202 */
    public static final String SECRET = "CMrx4202";
    /** token 过期时间: 1天 */
    public static final int calendarField = Calendar.DATE;
    public static final int calendarInterval = 1;

    public static String enToken(User user){
        Date iatDate = new Date();
        // expire time
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(calendarField, calendarInterval);
        Date expiresDate = nowTime.getTime();

        //****
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
        String time =   simpleDateFormat.format(expiresDate);
//        System.out.println(time);
        String it = simpleDateFormat.format(iatDate);
//        System.out.println(it);
        try {
            expiresDate = simpleDateFormat.parse(time);
            iatDate = simpleDateFormat.parse(it);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //*****
        // header Map
        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        String token = JWT.create().withHeader(map) // header
                .withClaim("userid", null == user.getUserid() ? "" : user.getUserid()) // payload
                .withClaim("userName", null == user.getUserName() ? "" : user.getUserName())
                .withClaim("userCard", null == user.getUserCard() ? "" : user.getUserCard())
                .withClaim("organName", null == user.getOrganName() ? "" : user.getOrganName())
                .withClaim("techid", null == user.getTechid() ? "" : user.getTechid())
                .withClaim("regionalism", null == user.getRegionalism() ? "" : user.getRegionalism())
               // .withClaim("user_id", null == user_id ? null : user_id.toString())
                .withIssuedAt(iatDate) // sign time
                .withExpiresAt(expiresDate) // expire time
                .sign(Algorithm.HMAC256(SECRET)); // signature
        return token;
    }

    public static User deToken(String token){
        User user = new User();
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
         DecodedJWT jwt = null;
        try {
            jwt = verifier.verify(token);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("errortoken==="+token);
//            throw new RuntimeException("凭证过期！");
        }
        Map<String, Claim> claims = jwt.getClaims();
        user.setUserid(claims.get("userid").asString());
        user.setUserName(claims.get("userName").asString());
        user.setUserCard(claims.get("userCard").asString());
        user.setOrganName(claims.get("organName").asString());
        user.setTechid(claims.get("techid").asString());
        user.setRegionalism(claims.get("regionalism").asString());
        return user;
    }


}
